package com.ruoyi.db.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.message.MsgUtils;
import com.ruoyi.db.constant.DbMsg;
import com.ruoyi.db.constant.DbStatus;
import com.ruoyi.db.domain.DbDb;
import com.ruoyi.db.service.IDbDbService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据库模型之库信息Controller
 * 
 * @author ruoyi
 * @date 2022-07-10
 */
@RestController
@RequestMapping("/db/db")
public class DbDbController extends BaseController
{
    @Autowired
    private IDbDbService dbDbService;

    /**
     * 查询数据库模型之库信息列表
     */
    @PreAuthorize("@ss.hasPermi('db:db:list')")
    @GetMapping("/list")
    public TableDataInfo list(DbDb dbDb)
    {
        startPage();
        List<DbDb> list = dbDbService.selectDbDbList(dbDb);
        return getDataTable(list);
    }

    /**
     * 导出数据库模型之库信息列表
     */
    @PreAuthorize("@ss.hasPermi('db:db:export')")
    @Log(title = "数据库模型之库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DbDb dbDb)
    {
        List<DbDb> list = dbDbService.selectDbDbList(dbDb);
        ExcelUtil<DbDb> util = new ExcelUtil<DbDb>(DbDb.class);
        util.exportExcel(response, list, "数据库模型之库信息数据");
    }

    /**
     * 获取数据库模型之库信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('db:db:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dbDbService.selectDbDbById(id));
    }

    /**
     * 新增数据库模型之库信息
     */
    @PreAuthorize("@ss.hasPermi('db:db:add')")
    @Log(title = "数据库模型之库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DbDb dbDb)
    {
        return toAjax(dbDbService.insertDbDb(dbDb));
    }

    /**
     * 修改数据库模型之库信息
     */
    @PreAuthorize("@ss.hasPermi('db:db:edit')")
    @Log(title = "数据库模型之库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DbDb dbDb)
    {
        return toAjax(dbDbService.updateDbDb(dbDb));
    }

    /**
     * 删除数据库模型之库信息
     */
    @PreAuthorize("@ss.hasPermi('db:db:remove')")
    @Log(title = "数据库模型之库信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
    	if(ids.length>1) {
    		return AjaxResult.error(MsgUtils.get(DbMsg.MULTIPLE_REMOVE_NOT_ALLOWED));
    	}
    	DbDb dbInfo = dbDbService.selectDbDbById(ids[0]);
    	if(DbStatus.MODELED_YES.getCode().equals(dbInfo.getStatus())) {
    		return AjaxResult.error(MsgUtils.get(DbMsg.MODELED_REMOVE_NOT_ALLOWED));
    	}
        return toAjax(dbDbService.deleteDbDbByIds(ids));
    }
    /**
     * 构建数据库模型
     */
    @PreAuthorize("@ss.hasPermi('db:db:build')")
    @Log(title = "数据库模型之库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/build/{id}")
    public AjaxResult build(@PathVariable Long id)
    {
    	DbDb dbInfo = dbDbService.selectDbDbById(id);
    	//TODO 数据校验
    	dbDbService.buildDbModel(dbInfo);
    	dbInfo.setStatus(DbStatus.MODELED_YES.getCode());
    	int updateDbDb = dbDbService.updateDbDb(dbInfo);
    	return toAjax(updateDbDb);
    }
}
